<template>
  <el-upload
  class="upload-demo"
  action="http://certfileupload.oss-cn-beijing.aliyuncs.com/"
  :on-preview="handlePreview"
  :before-upload="upLoad"
  :data="objData"
  :on-success="uploadSuccess"
  :fileList="fileList"
  ref="uploadRef"
  >
  <el-button size="small" type="primary" :disabled="isdisabled">点击上传</el-button>
</el-upload>
</template>

<script>
let Base64 = require('js-base64').Base64
export default {
 data() {
      return {
      objData: {
         OSSAccessKeyId: 'LTAI5tQi7EpJ2U5J7FDyDehj',
         policy: 'eyJleHBpcmF0aW9uIjoiMjAyMy0wMS0wMVQxMjowMDowMC4wMDBaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF1dfQ==',
         signature: 'LvgzUVRl4yFstGWPlgWvO2wTQp8=',
         success_action_status: 200,
         key: ''
       },
       attachmentId: 0,
       path: '',
       fileList:[],
       fileName: ''
      };
    },
    props: {
      isdisabled: false
    },
    methods: {
      handlePreview(file) {
        this.$http({
          url:this.$http.fileUpload(`/file/admin/info/${this.attachmentId}`),
          method: "get"
        }).then(res => {
          this.path=res.data.data.path
          this.fileName = this.path.substring(this.path.lastIndexOf('/') + 1);
          window.open('https://file.keking.cn/onlinePreview?url='+encodeURIComponent(Base64.encode(this.path)))
        })
      },
      async getUrl(id) {
        this.attachmentId = id
        let res = await this.$http({
          url:this.$http.fileUpload(`/file/admin/info/${id}`),
          method: "get"
        })
        this.path=res.data.data.path
        this.fileName = this.path.substring(this.path.lastIndexOf('/') + 1);
        this.$set(this.fileList, 0,{
            name: this.fileName,
            url: 'https://certfileupload.oss-cn-beijing.aliyuncs.com/'+ `${this.fileName}`
        })
      },
      upLoad(file) {
        this.objData.key = "${filename}"
      },
      uploadSuccess(response, file) {
        this.$http({
          url:this.$http.fileUpload("/file/upload"),
          method: "POST",
          params: this.$http.adornParams({
          url: 'https://certfileupload.oss-cn-beijing.aliyuncs.com/'+ `${file.name}`
        },
        false
        )
        }).then(res => {
          this.attachmentId = res.data.data
          this.fileName = file.name
            this.$set(this.fileList, 0,{
            name: this.fileName,
            url: 'https://certfileupload.oss-cn-beijing.aliyuncs.com/'+ `${this.fileName}`
          })
          this.$emit('setCertAttachmentId', this.attachmentId)
        })
      },
      // 清除list
      clearFiles() {
        this.fileList = []
      }
    }
}
</script>

<style>

</style>
